package com.unrarme.api.antigate
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	import mx.utils.Base64Encoder;
	
	public class Antigate extends EventDispatcher
	{
		private var requestURL:String = 'http://antigate.com/in.php';
		private var responseURL:String = 'http://antigate.com/res.php';
		//private var responseURL:String = 'http://antigate.com/res.php?key={key}&action=get&id=';
		
		private var key:String = '';
		private var softId:String = '';
		
		public var captchaId:String = '';
		private var captchaUrl:String = ''; 
		
		public var phrase:Number = 0;
		public var regsense:Number = 0;
		public var numeric:Number = 0;
		public var calc:Number = 0;
		public var minLen:Number = 0;
		public var maxLen:Number = 0;
		public var isRussian:Number = 0;
		
		public var ext:String = 'jpg';
		
		
		public function Antigate(key:String)
		{
			this.key = key;
		}
		
		public function parseCaptchaFromURL(url:String = ''):void {
			if ((url == '')&&(this.captchaUrl != '')) {
				url = this.captchaUrl;
			} else if (url != '') {
				this.captchaUrl = url;
			}
			var loader:URLLoader = new URLLoader();
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			loader.addEventListener(Event.COMPLETE, onCaptchaImageLoaded);
			var request:URLRequest = new URLRequest(url);
			loader.load(request);
		}
		
		private function onCaptchaImageLoaded(e:Event):void {
			var image:ByteArray = URLLoader(e.target).data;
			var encoder:Base64Encoder = new Base64Encoder();
			encoder.encodeBytes(image);
			
			var request:URLRequest = new URLRequest(requestURL);
			request.method = URLRequestMethod.POST;
			
			var postData:URLVariables = new URLVariables();
			postData.method = 'base64';
			postData.key = this.key;
			postData.body = encoder.flush();
			postData.ext = this.ext;
			
			postData.phrase = this.phrase;
			postData.regsense = this.regsense;
			postData.numeric = this.numeric;
			postData.calc = this.calc;
			postData.min_len = this.minLen;
			postData.max_len = this.maxLen;
			postData.is_russian = this.isRussian;
			if (this.softId != '') {
				postData.soft_id = this.softId;
			}
			
			request.data = postData;
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onCaptchaSent);
			loader.load(request);
		}
		
		private function onCaptchaSent(e:Event):void {
			var data:String = URLLoader(e.target).data;
			var result:Array = data.split('|');
			if(result[0] == 'OK') {
				this.captchaId = result[1];
				var timer:Timer = new Timer(10000, 1);
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
				timer.start();
				dispatchEvent(new AntigateEvent(AntigateEvent.SENT_OK));
			} else {
				parseCaptchaFromURL();
				var ae:AntigateEvent = new AntigateEvent(AntigateEvent.SENT_ERROR);
				ae.data = data;
				dispatchEvent(ae);
			}
		}
		
		private function onTimerComplete(e:TimerEvent):void {
			var request:URLRequest = new URLRequest(responseURL);
			request.method = URLRequestMethod.GET;
			
			var getData:URLVariables = new URLVariables();
			getData.key = this.key;
			getData.action = 'get';
			getData.id = this.captchaId;
			if (this.softId != '') {
				getData.soft_id = this.softId;
			}
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onResponseRecieved);
			loader.load(request);
		}
		
		private function onResponseRecieved(e:Event):void {
			var data:String = URLLoader(e.target).data;
			var result:Array = data.split('|');
			
			var ae:AntigateEvent = new AntigateEvent(AntigateEvent.RESPONSE_OK);
			
			if (result[0] == 'OK') {
				ae.data = result[1];
				dispatchEvent(ae);
			} else {
				ae = new AntigateEvent(AntigateEvent.RESPONSE_ERROR);
				ae.data = data;
				dispatchEvent(ae);
				var timer:Timer = new Timer(5000);
				timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
				timer.start();
			}
			
		}
		
		
	}
}